package com.kinggm.base.aspect;

import com.kinggm.annotation.DataSourceName;
import com.kinggm.base.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 *
 * DynamicDataSource类的切面类
 *
 * */
@Component
@Aspect
@Slf4j
public class DynamicDataSourceAspect {

    // 使用前置通知 切换数据源
    // 判断com.kinggm.service.impl包下所有使用@DataSourceName注解的方法或者类
    @Before("@annotation(dataSourceName)")
    public void before(JoinPoint point, DataSourceName dataSourceName){
        String value = dataSourceName.value();
        log.info("当前数据源：{}",value);
        DynamicDataSource.dataSourceName.set(value);
    }


}
